package com.immunology.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.immunology.service.UserService;

@Service(value = "userDetailService")
public class UserDetailsServiceImpl implements UserDetailsService {

    private static final Logger LOG = Logger
	    .getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String login)
	    throws UsernameNotFoundException, DataAccessException {
	LOG.info("Load User by userName...");
	com.immunology.domain.User user = userService.getByLogin(login);
	User userDetail = null;
	if (user == null) {
	    LOG.error("Can't find user by userName: '" + login + "'");
	    throw new UsernameNotFoundException("Invalid userName.");
	} else {
	    userDetail = new User(login, user.getPassword(), true, true, true,
		    true, user.getRoles());
	}
	return userDetail;
    }

}
